//
//  NewRansomController.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/4.
//

#import "NewRansomController.h"
#import "HandleNetProduct+Transaction.h"
#import "RansomNewController.h"
#import "CKPuslichTool.h"
#import "UIColor+NN.h"
#import "UserModel.h"
#import "TKAlertCenter.h"
#import "RansomOrderCell.h"
#import "RansomMangerController.h"
#import "TransactionController.h"

@interface NewRansomController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myNewTable;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic, strong) NewRansomOrderModel *createModel;
@property (nonatomic, strong) NSDictionary *ransomCheckDic;
@property (nonatomic, copy) NSString *ransomShare;

@property (nonatomic, assign) BOOL isSHow;

@end

@implementation NewRansomController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建赎回单";
    self.myNewTable.tableFooterView = _bottomView;
    
    if (_redeemId) {
        [self getRansomEditInfoAction];
        self.title = @"编辑赎回单";
    }
    else {
        [self getRansomNewInfoAction];
    }
}

//赎回新建显示
- (void)getRansomNewInfoAction {
    [HandleNetProduct getRansomNewContractNo:_contractNo Success:^(NSDictionary * _Nonnull data) {
        _createModel = [NewRansomOrderModel mj_objectWithKeyValues:data];
        [_myNewTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

//赎回份额检查
- (void)checkRansomShareAction:(NSString *)share {
    NSDictionary *param = @{@"PartRedeem": _createModel.PartRedeem,
                            @"Redeemshare": share,
                            @"OpenShare": _createModel.OpenShare,
                            @"ClosedShare": _createModel.ClosedShare,
                            @"ContractNo": _createModel.ContractNo,
                            @"IsProductClose": _createModel.ClosePeriodRedeem};
    [HandleNetProduct getRansomShareCheck:param Success:^(NSDictionary * _Nonnull data) {
        _ransomCheckDic = data;
        _createModel.ReferenceAmount = data[@"ReferenceAmount"];
        [_myNewTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        _createModel.Redeemshare = @"";
        _createModel.ReferenceAmount = @"";
        [_myNewTable reloadData];
    }];
}

//赎回单编辑显示
- (void)getRansomEditInfoAction {
    [HandleNetProduct getRansomEditRansomId:_redeemId Success:^(NSDictionary * _Nonnull data) {
        _createModel = [NewRansomOrderModel mj_objectWithKeyValues:data];
        _ransomShare = _createModel.Redeemshare;
        [_myNewTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

//赎回编辑份额
- (void)editRansomShareAction:(NSString *)share {
    NSDictionary *param = @{@"PartRedeem": _createModel.PartRedeem,
                            @"Redeemshare": share,
                            @"OpenShare": _createModel.OpenShare,
                            @"ClosedShare": _createModel.ClosedShare,
                            @"ContractNo": _createModel.ContractNo,
                            @"IsProductClose": @"",
                            @"RedeemId": _redeemId};
    [HandleNetProduct editRansomShareCheck:param Success:^(NSDictionary * _Nonnull data) {
        _ransomCheckDic = data;
        _createModel.ReferenceAmount = data[@"ReferenceAmount"];
        [_myNewTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        _createModel.Redeemshare = @"";
        _createModel.ReferenceAmount = @"";
        [_myNewTable reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 11;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak NewRansomController *weakSelf = self;
    RansomOrderCell *cell = nil;
    if (indexPath.row == 7) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"RansomOrderCell1"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (!_isSHow && [self.title isEqualToString:@"编辑赎回单"]) {
                [cell.textField becomeFirstResponder];
                _isSHow = YES;
            }
        });
        
        cell.returnBlock = ^(NSString * _Nonnull share) {
            if ([share floatValue] > [[_createModel.OpenShare stringByReplacingOccurrencesOfString:@"," withString:@""] floatValue]) {
                _createModel.Redeemshare = @"";
                [[TKAlertCenter defaultCenter]postAlertWithMessage:@"赎回份额不能超过开放份额。"];
                [_myNewTable reloadData];
            }
            else {
                _createModel.Redeemshare = share;
                if (_redeemId) {
                    [weakSelf editRansomShareAction: share];
                }
                else {
                    [weakSelf checkRansomShareAction: share];
                }
            }
        };
    }
    else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"RansomOrderCell"];
    }
    [cell setNewRansomOrderAction:_createModel index:indexPath.row];
    return cell;
}

- (IBAction)confirmRansomAction:(UIButton *)sender {
    __weak NewRansomController *weakSelf = self;
    if ([_ransomCheckDic[@"IsBooking"] integerValue] == 0) {
        [self junpRansomAction];
        return;
    }
    if (_redeemId) {
        if (!_createModel.Redeemshare) {
            [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入赎回份额"];
            return;
        }
        if ([_createModel.Redeemshare floatValue] == [_ransomShare floatValue]) {
            RansomMangerController *manage = nil;
            TransactionController *transaction = nil;
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[RansomMangerController class]]) {
                    manage = (RansomMangerController *)controller;
                }
                if ([controller isKindOfClass:[TransactionController class]]) {
                    transaction = (TransactionController *)controller;
                }
            }
            weakSelf.navigationController.viewControllers = @[transaction, manage];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            return;
        }
        UserModel *model=[UserModel getUser];
        int mid=[model.CustomerID intValue];
        NSDictionary *param = @{@"ReDeemId": _redeemId,
                                @"Redeemshare": _createModel.Redeemshare,
                                @"RedeemFee": _ransomCheckDic[@"RedeemFee"],
                                @"SYShare": _ransomCheckDic[@"SYShare"],
                                @"SYAmount": _ransomCheckDic[@"SYAmount"],
                                @"ProductId": _createModel.ProductId,
                                @"SubProductId": _createModel.SubProductId,
                                @"AccountId": _createModel.CustomerID,
                                @"OpenDate": _createModel.RedeemOpenDayShow,
                                @"Currency": _createModel.Currency,
                                @"ReferenceAmount": _createModel.ReferenceAmount,
                                @"FaId": @(mid),
                                @"ContractNo": _createModel.ContractNo};
        [HandleNetProduct saveRansomShare:param Success:^(NSDictionary * _Nonnull data) {
            [[TKAlertCenter defaultCenter]postAlertWithMessage:@"修改成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                RansomMangerController *manage = nil;
                TransactionController *transaction = nil;
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[RansomMangerController class]]) {
                        manage = (RansomMangerController *)controller;
                    }
                    if ([controller isKindOfClass:[TransactionController class]]) {
                        transaction = (TransactionController *)controller;
                    }
                }
                weakSelf.navigationController.viewControllers = @[transaction, manage];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        } Failure:^(NSString * _Nonnull errorInfo) {
            
        }];
    }
    else {
        if (!_createModel.Redeemshare) {
            [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入赎回份额"];
            return;
        }
        UserModel *model=[UserModel getUser];
        int mid=[model.CustomerID intValue];
        NSDictionary *param = @{@"Redeemshare": _createModel.Redeemshare,
                                @"RedeemFee": _ransomCheckDic[@"RedeemFee"],
                                @"SYShare": _ransomCheckDic[@"SYShare"],
                                @"SYAmount": _ransomCheckDic[@"SYAmount"],
                                @"ProductId": _createModel.ProductId,
                                @"SubProductId": _createModel.SubProductId,
                                @"AccountId": _createModel.CustomerID,
                                @"OpenDate": _createModel.RedeemOpenDayShow,
                                @"Currency": _createModel.Currency,
                                @"ReferenceAmount": _createModel.ReferenceAmount,
                                @"FaId": @(mid),
                                @"ContractNo": _createModel.ContractNo};
        [HandleNetProduct addRansomNew:param Success:^(NSDictionary * _Nonnull data) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } Failure:^(NSString * _Nonnull errorInfo) {
            
        }];
    }
}

- (void)junpRansomAction {
    RansomNewController *vc = (RansomNewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomNewController"];
    vc.isDetail = NO;
    vc.redeemId = _redeemId;
    vc.createModel = _createModel;
    vc.ransomCheckDic = _ransomCheckDic;
    vc.ransomShare = _ransomShare;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
